﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.OleDb;

namespace WirtualneNauczanie.Web
{
    public partial class RegisterUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RegisterButton_Click(object sender, EventArgs e)
        {
            MembershipCreateStatus createStatus;
            //MembershipUser newUser = Membership.CreateUser(Login.Text, Password.Text, Email.Text);
            
            //MembershipUser newUser = Membership.CreateUser(Login.Text, Password.Text, Email.Text, Question.Text, Answer.Text, true, out createStatus);
            MembershipUser newUser = Membership.CreateUser(Login.Text, Password.Text, Email.Text, "question", "answer", true, out createStatus);
            switch (createStatus)
            {
                case MembershipCreateStatus.Success:
                    WarningLabel.Text = "Udało się zarejestrować użytkownika! ";
                    Roles.AddUserToRole(newUser.UserName, "Student");
                    addStudentToDb(newUser);
                    LinkButton1.Visible = true;
                    break;
                case MembershipCreateStatus.DuplicateUserName:
                    WarningLabel.Text = "Użytkownik o podanym loginie istnieje!";
                    break;
                case MembershipCreateStatus.InvalidEmail:
                    WarningLabel.Text = "Nie poprawny e-mail!";
                    break;
                case MembershipCreateStatus.DuplicateEmail:
                    WarningLabel.Text = "e-mail o podanej nazwie już istnieje!";
                    break;
                default:
                    break;
            }
        }

    private void addStudentToDb(MembershipUser user) {
        //ObjectDataSource1.Insert("1", user.UserName, "bobik", user.Email);
       
      /*  SqlDataSource1.InsertParameters.Add("@StudentID", "1");
        SqlDataSource1.InsertParameters.Add("@StudentName", user.UserName);
        SqlDataSource1.InsertParameters.Add("@Password", "bobik");
        SqlDataSource1.InsertParameters.Add("@EmailAddress", user.Email);*/
        
       /* SqlDataSource1.Insert();                */

       /* using (OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;DATA SOURCE=" + MapPath("~/App_Data/BazaDanych.mdf")))
        {
            con.Open();

            OleDbCommand cmd = new OleDbCommand("SELECT MAX( StudentID ) FROM Students", con);
            int id = (int)cmd.ExecuteScalar();

            cmd = new OleDbCommand("INSERT INTO Students( StudentID, Login, Email) VALUES ( @StudentID, @Login, @Email", con);
            cmd.Parameters.Add("StudentID", OleDbType.Integer).Value = id + 1;

            cmd.Parameters.Add("Login", OleDbType.VarChar).Value = user.UserName;
            cmd.Parameters.Add("Email", OleDbType.VarChar).Value = user.Email;

            cmd.ExecuteNonQuery();
        }*/

    }
    }
}